package com.xlh.controller.course;

import com.google.common.collect.Lists;
import com.xlh.common.Result;
import com.xlh.dto.course.ChapterTechnologyIndexDTO;
import com.xlh.dto.course.TechnologyDTO;
import com.xlh.param.course.TechnologyParam;
import com.xlh.service.course.TechnologyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 技能
 * <p>
 * Created by lx on 2019/1/25.
 */
@Api(tags = "TechnologyController", description = "技能")
@RestController
@RequestMapping("/technology")
public class TechnologyController {

    @Autowired
    private TechnologyService technologyService;

    @ApiOperation("查询技能所有数据")
    @GetMapping("/list")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result<List<TechnologyDTO>> listTechnology() {
        return Result.success(technologyService.queryTechnologyFrequency());
    }

    @ApiOperation("添加技能")
    @PostMapping
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result createTechnology(@RequestBody @ApiParam(value = "添加技能的参数", required = true) @Valid TechnologyParam param) {
        technologyService.createTechnology(param);
        return Result.success();
    }

    @ApiOperation("编辑技能")
    @PutMapping("/{id}")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result updateTechnology(
            @PathVariable("id") @ApiParam(value = "技能id", required = true) Long technologyId,
            @RequestBody @ApiParam(value = "编辑技能的参数", required = true) @Valid TechnologyParam param) {
        technologyService.updateTechnology(technologyId, param);
        return Result.success();
    }

    @ApiOperation("删除技能")
    @DeleteMapping("/{id}")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result deleteBatchTechnology(
            @PathVariable("id") @ApiParam(value = "技能id", required = true) Long technologyId) {
        technologyService.batchDeleteTechnology(Lists.newArrayList(technologyId));
        return Result.success();
    }

    @ApiOperation("某技能下的章节列表")
    @GetMapping("/{id}/chapter")
    @PreAuthorize("@permissionValidator.isTeacherOrStudent()")
    public Result<ChapterTechnologyIndexDTO> queryTechnologyChapter(
            @PathVariable("id") @ApiParam(value = "技能id", required = true) Long technologyId) {
        return Result.success(technologyService.queryTechnologyChapter(technologyId));
    }

}
